package ar.edu.unrn.lia.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

@Entity
@Table(
        name="user", uniqueConstraints =       @UniqueConstraint (columnNames={"username"})
    )
public class User extends BaseEntity implements Serializable, UserDetails {

	/**
	 * 
	 */
	private static final long serialVersionUID = 327336392782096608L;
	
	private String username;
	private String password;
	private String firstName;
	private boolean active = true;
	//	Se usa cuando el usuario maneja varios roles.
	// private List<Role> role;
	private Role role;

	
	@NotNull(message = "{name.notnull}")
	@Size(min = 4, max = 30, message = "{name.size}")
	@Column(name = "password", nullable = true)
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Size(min = 4, max = 30, message = "{name.size}")
	@Column(name = "username", unique = true, nullable = true)
	@NotNull(message = "{name.notnull}")
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

//	Se usa cuando el usuario maneja varios roles.
	// @ManyToMany(fetch = FetchType.EAGER)
	// @JoinTable(name = "user_role", joinColumns = @JoinColumn(name =
	// "id_user"), inverseJoinColumns = @JoinColumn(name = "id_role"))
	// public List<Role> getRole() {
	// return role;
	// }
	//
	// public void setRole(List<Role> role) {
	// this.role = role;
	// }

	@Transient
	public boolean isAccountNonExpired() {
		return true;
	}

	@Transient
	public boolean isAccountNonLocked() {
		return true;
	}

	@Transient
	public boolean isCredentialsNonExpired() {
		return true;
	}

	// @Transient
	public boolean isEnabled() {
		return active;
	}

	public void setEnabled(boolean active) {
		this.active = active;
	}

	@Transient
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> result = new ArrayList<GrantedAuthority>();
		// for (Role role : roles) {
		// result.add(role);
		// }
		result.add(this.getRole());
		return result;

	}

	@NotNull(message = "{name.notnull}")
	@ManyToOne(optional = false, fetch = FetchType.EAGER)
	@JoinColumn(name = "role_id", referencedColumnName = "id")
	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}
	@NotNull(message = "{name.notnull}")
	@Size(min = 4, max = 40, message = "{name.size}")
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}


}
